/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sg.edu.nus.iss.pacman.form;

import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import sg.edu.nus.iss.pacman.dao.Score;
import sg.edu.nus.iss.pacman.dao.UserProfile;
import sg.edu.nus.iss.pacman.midlet.PacManMIDlet;

/**
 * User Profile Option List.
 * @author daniel, fredy
 */
public class UserProfileOptionList extends List implements CommandListener
{
    private static final String TITLE = "Your score is ";
	private PacManMIDlet mainApp;

	private Command cmdSelect;

	private String[] choices = new String[]{"<Create New>"};
    private UserProfile[] userProfile;
    private Score score;

    public UserProfileOptionList(PacManMIDlet mainApp, UserProfile[] userProfile, Score score)
    {
        super(TITLE + new Integer(score.getScore()).toString(), List.IMPLICIT, new String[]{}, null);
        this.mainApp = mainApp;
        this.userProfile = userProfile;
        this.score = score;
        init();
    }

    private void init()
    {
        insert(0, choices[0], null);
        for(int i = 0; i < userProfile.length; i++)
        {
            insert((i+1), userProfile[i].getName(), null);
        }
        
        cmdSelect = new Command(PacManMIDlet.LABEL_CMD_SELECT, Command.OK, 1);
        addCommand(cmdSelect);
        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d)
    {
        if (cmdSelect == c)
        {
            int idx = getSelectedIndex();
            if (0 == idx)
            {
                mainApp.displayEnterUserProfileForm();
            }
            else
            {
                mainApp.saveScore(userProfile[idx - 1]);
            }
        }
    }
}
